<?php
/**
 * Created by PhpStorm.
 * User: LiuJiaJun
 * Date: 2018/11/23
 * Time: 17:35
 */

namespace api\storage\controller;

use api\storage\common\Constant;
use cmf\controller\RestBaseController;
use think\Cache;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\Request;

class AppBaseController extends RestBaseController
{
    /**
     * 仓储员uuid
     * @var
     */
    private $uuid;
    /**
     * 仓库的uuid信息
     * @var
     */
    private $storageUuid;
    /**
     * 仓库的类型
     * @var
     */
    private $storageType;
    /**
     * 用户的账号信息
     * @var
     */
    private $username;
    /**
     * 用户昵称
     * @var
     */
    private $nickname;
    /**
     * 用户所属的角色信息
     * @var
     */
    private $roleId;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
    }

    protected function _initialize()
    {
        parent::_initialize();
        $userId = $this->getUserId();
        try {
            $user = Cache::get(Constant::CACHE_PREFIX_USER.$userId);
            if (empty($user)) {
                $user = Db::name('user')
                    ->alias('user')
                    ->join(['cmf_role_user' => 'role'], 'user.id = role.user_id', 'left')
                    ->join(['iwh_staff_storage' => 'ss'], 'user.id = ss.user_id', 'left')
                    ->join(['iwh_storage' => 's'], 'ss.storage_uuid = s.uuid', 'left')
                    ->field('user.*,role.role_id,ss.storage_uuid,s.type as storage_type')
                    ->where('user.id', $userId)
                    ->where('ss.is_current', 1)
                    ->find();
                Cache::set(Constant::CACHE_PREFIX_USER.$userId, $user);
                //把仓库信息给缓存起来
                $storages = Db::table('iwh_storage')->select();
                foreach ($storages as $storage){
                    Cache::set(Constant::CACHE_PREFIX_STORAGE.$storage['uuid'],$storage);
                }
            }
            if (!empty($user)) {
                $this->storageUuid = $user['storage_uuid'];
                $this->storageType = $user['storage_type'];
                $this->username = $user['user_login'];
                $this->nickname = $user['user_nickname'];
                $this->roleId = $user['role_id'];
                $this->uuid = $user['uuid'];
            }
        } catch (DataNotFoundException $e) {
            $this->error($e->getMessage());
        } catch (ModelNotFoundException $e) {
            $this->error($e->getMessage());
        } catch (DbException $e) {
            $this->error($e->getMessage());
        }
    }

    /**
     * @return mixed
     */
    public function getStorageUuid()
    {
        return $this->storageUuid;
    }

    /**
     * @return mixed
     */
    public function getUsername()
    {
        return $this->username;
    }

    /**
     * @return mixed
     */
    public function getRoleId()
    {
        return $this->roleId;
    }

    /**
     * @return mixed
     */
    public function getNickname()
    {
        return $this->nickname;
    }

    public function getStorageType(){
        return $this->storageType;
    }

    public function getUuid()
    {
        return $this->uuid;
    }


    /**
     * 将下划线命名转换为驼峰式命名
     *
     * @param $str
     * @param bool $ucfirst
     * @return mixed|string
     */
    protected function convertUnderline($str , $ucfirst = false)
    {
        $str = ucwords(str_replace('_', ' ', $str));
        $str = str_replace(' ','',lcfirst($str));
        return $ucfirst ? ucfirst($str) : $str;
    }

    protected function convertResult(array $data){
        $result = [];
        foreach ($data as $key=>$value) {
            foreach ($value as $oldK=>$v){
                $newK = $this->convertUnderline($oldK);
                $result[$key][$newK] = $v;
            }
        }
        return $result;
    }

    /**
     * json解析成数组
     *
     * @param $str
     * @return mixed
     */
    protected function jsonDecode($str){
        return json_decode($str,true);
    }
}